package com.neusoft.training.podcast.controller;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.training.podcast.entity.Category;
import com.neusoft.training.podcast.entity.Comment;
import com.neusoft.training.podcast.entity.Favourite;
import com.neusoft.training.podcast.entity.User;
import com.neusoft.training.podcast.entity.Video;
import com.neusoft.training.podcast.service.CategoryService;
import com.neusoft.training.podcast.service.CommentService;
import com.neusoft.training.podcast.service.FavouriteService;
import com.neusoft.training.podcast.service.UserService;
import com.neusoft.training.podcast.service.VideoService;


@Controller

public class PersonController {
	@Autowired
	private CommentService commentservice;
	@Autowired
	private UserService userservice;
	@Autowired
	private FavouriteService favouriteservice;
	@Autowired
	private VideoService videoservice;
	@Autowired
	private CategoryService categoryservice;

//个人信息
//pass
@RequestMapping(value="/modifyInfo",method=RequestMethod.POST)
public @ResponseBody User modifyinfo(String Oldname,String name,String password,String phoneNum, String email,HttpSession session) {
	   
	   User user;
	   System.out.println("enter");
	   session.setAttribute("key", null);
	   user=userservice.modifyUserInfo(Oldname,name,password,phoneNum,email);
	   session.setAttribute("key", user);
	   return user;

}


@RequestMapping(value="/deletelComment",method=RequestMethod.POST)
public @ResponseBody String deleteComment(Integer commentId)
{
	
	commentservice.deleteComment(commentId);
	return "success delete";
	}
//pass

@RequestMapping(value="/searchComment",method=RequestMethod.POST)
public @ResponseBody Comment searchComment(Integer commentId)
{
	Comment comment=null;
	comment=commentservice.findByID(commentId);
	return comment;
	}

//pass
@RequestMapping(value="/findByContex",method=RequestMethod.POST)
public @ResponseBody List<Comment> findByContex(String context)
{
	List<Comment> comment=null;
	comment=commentservice.findCommentByContext(context);
	return comment;
	}

//收藏夹
//pass

//pass
@RequestMapping(value="/deleteFavourite",method=RequestMethod.POST)
public @ResponseBody String deleteFavourite(String Videoname) {
	favouriteservice.deleteFavourite(Videoname);
	return "delete success";
}


@RequestMapping(value="/showFavourite",method=RequestMethod.POST)
public @ResponseBody Map<String,Object> findFavouriteByUser(Integer userID)
{
	Map<String, Object> map=new HashMap<String,Object>();
	System.out.println(userID);
	List<Favourite> favourites = favouriteservice.findFavouriteByUserID(userID);
	map.put("favourite", favourites);
	System.out.println("favourite"+favourites.size());
	List<Video> videos = videoservice.findFavouriteVideoByID(userID);
	map.put("favouriteVedioData", videos);
	System.out.println("video"+videos.size());
	List<Category> category = categoryservice.findFavouriteCategories(userID);
	map.put("favouriteCategoryData", category);
	System.out.println("category"+category.size());
	List<User> user=userservice.findVideoUser(userID);
	map.put("vedioUserData" , user);
	System.out.println("user"+user.size());
	return map;
	}

@RequestMapping(value="/showPersonVideo",method=RequestMethod.POST)
public @ResponseBody Map<String,Object> showPersonVideo(Integer userID)
{
	System.out.println("personvideocomment");
	Map<String, Object> map=new HashMap<String,Object>();
	
	List<Video> video = videoservice.findVideoOfAuthor(userID);
	System.out.println("video"+video.size());
	map.put("vedioData", video);
	
	for(int i=0;i<video.size();i++)
	{
		 System.out.println(video.get(i).getVideoID());
	}
	
	List<User> user = userservice.findCommentUser(userID);
	System.out.println("user"+user.size());
	map.put("vedioCommentUserData", user);
	
	List<Category> category = categoryservice.findPersonVideoCategory(userID);
	map.put("categoryData",category);
	System.out.println("category"+category.size());
	
	List<Comment> comment = commentservice.findPersonVideoComment(userID);
	System.out.println("comment"+comment.size());
	map.put("vedioCommentData",comment);
	
	ArrayList<String> videoID=new ArrayList<>();
	for(int i=0;i<video.size();i++)
	{
	    System.out.println(video.get(i).getVideoID());
		videoID.add(video.get(i).getVideoID());
	}
	ArrayList<Integer> count=commentservice.findCommentCount(videoID);
	System.out.println("count"+count.size());
	map.put("count",count);
	return map;
	}

@RequestMapping(value="/showPersonComments",method=RequestMethod.POST)
public @ResponseBody Map<String,Object> showPersonComments(Integer userID)
{
	Map<String, Object> map=new HashMap<String,Object>();
	User user = userservice.findUserByID(userID);
	map.put("selfUserData", user);
	System.out.println("user"+"1");
	
	List<User> otheruser=userservice.findOtheruser(userID);
	map.put("othersUserData", otheruser);
	System.out.println("otheruser"+otheruser.size());
	
	List<Comment> selfComment = commentservice.findSelfComment(userID);
	map.put("selfComment", selfComment);
	System.out.println("selfComment"+selfComment.size());
	
	List<Comment> otherComment=commentservice.findOtherComment(userID);
	map.put("otherComment", otherComment);
	System.out.println("otherComment"+otherComment.size());
	
    List<Video> video = videoservice.findCommentVideo(userID);
    map.put("video",video);
	System.out.println("video"+video.size());
    
    List<Category> category = categoryservice.findCommentVideoCategory(userID);
    map.put("category", category);
    System.out.println("category"+category.size());
    
    List<User> Videoauthor = userservice.findCommentVideoUser(userID);
    map.put("videoauthor", Videoauthor);
    System.out.println("category"+category.size());
    
	return map;
	
	}
}
